package com.ruoyi.binance.service;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.binance.api.OrderApi;
import com.ruoyi.binance.domain.BinanceAutoSingleCell;
import com.ruoyi.binance.domain.BinanceTransactionConf;
import com.ruoyi.binance.domain.BinanceTransactionOrder;
import com.ruoyi.binance.mapper.BinanceAutoSingleCellMapper;
import com.ruoyi.binance.mapper.BinanceTransactionConfMapper;
import com.ruoyi.binance.mapper.BinanceTransactionOrderMapper;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

@Component
public class AsyncAutoSingleCell {
    @Autowired
    private BinanceAutoSingleCellMapper binanceAutoSingleCellMapper;
    @Autowired
    private BinanceTransactionOrderMapper binanceTransactionOrderMapper;
    @Autowired
    private BinanceTransactionConfMapper binanceTransactionConfMapper;

    @Async
    public void getAPriceMonitoring(Long confId, String code, Date time, BigDecimal close, BigDecimal open) {

        BinanceAutoSingleCell binanceAutoSingleCell = binanceAutoSingleCellMapper.selectBinanceAutoSingleCellByCode(code);
        if (binanceAutoSingleCell == null) {
            return;
        }
        //收盘价小于等于买入价格
        if (close.compareTo(binanceAutoSingleCell.getBuyPric()) <= 0) {
            //进行下单
            BinanceTransactionConf binanceTransactionConf = binanceTransactionConfMapper.selectBinanceTransactionConfById(confId);
            if (binanceTransactionConf != null && binanceTransactionConf.getUsdtNumber().compareTo(BigDecimal.ZERO) > 0
                    && !"BUY".equals(binanceTransactionConf.getSide())) {

                BinanceTransactionConf bean = new BinanceTransactionConf();
                bean.setId(binanceTransactionConf.getId());
                bean.setSide("BUY");
                binanceTransactionConfMapper.updateBinanceTransactionConf(bean);

                BinanceTransactionOrder order = new BinanceTransactionOrder();
                order.setCode(code);
                order.setOrgOrderId(IdUtil.getSnowflakeNextIdStr());
                order.setQuoteOrderQty(binanceTransactionConf.getUsdtNumber());
                order.setSide("BUY");
                order.setType("MARKET");
                order.setConfigId(confId);
                order.setCreateTime(time);
                order.setRemark("自动交易");
                int save = binanceTransactionOrderMapper.insertBinanceTransactionOrder(order);
                if (save > 0) {
                    //调用下单接口
                    JSONObject res = OrderApi.order(order);
                    if (StringUtils.isNotBlank(res.getString("code"))) {
                        order.setStatus("REJECTED");
                        order.setRemark(res.getString("msg"));
                    } else {
                        order.setStatus("PENDING_NEW");
                        order.setOrderId(res.getString("orderId"));
                    }
                    binanceTransactionOrderMapper.updateBinanceTransactionOrder(order);
                }
            }
        }
        //收盘价大于等于卖出价格
        if (close.compareTo(binanceAutoSingleCell.getSellPric()) >= 0) {
            //进行下单
            BinanceTransactionConf binanceTransactionConf = binanceTransactionConfMapper.selectBinanceTransactionConfById(confId);
            if (binanceTransactionConf != null && binanceTransactionConf.getCoinNumber().compareTo(BigDecimal.ZERO) > 0
                    && !"SELL".equals(binanceTransactionConf.getSide())) {

                BinanceTransactionConf bean = new BinanceTransactionConf();
                bean.setId(binanceTransactionConf.getId());
                bean.setSide("SELL");
                binanceTransactionConfMapper.updateBinanceTransactionConf(bean);

                BinanceTransactionOrder order = new BinanceTransactionOrder();
                order.setCode(code);
                order.setOrgOrderId(IdUtil.getSnowflakeNextIdStr());
                order.setQuantity(binanceTransactionConf.getCoinNumber());
                order.setSide("SELL");
                order.setType("MARKET");
                order.setConfigId(confId);
                order.setCreateTime(time);
                order.setRemark("自动交易");
                int save = binanceTransactionOrderMapper.insertBinanceTransactionOrder(order);
                if (save > 0) {
                    //调用下单接口
                    JSONObject res = OrderApi.order(order);
                    if (StringUtils.isNotBlank(res.getString("code"))) {
                        order.setStatus("REJECTED");
                        order.setRemark(res.getString("msg"));
                    } else {
                        order.setStatus("PENDING_NEW");
                        order.setOrderId(res.getString("orderId"));
                    }
                    binanceTransactionOrderMapper.updateBinanceTransactionOrder(order);
                }
            }
        }
    }
}
